package threadTest.CASTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class Test {

    public static void main(String[] args) {
        ReentrantLock reentrantLock = new ReentrantLock();
        //reentrantLock.lock();
        //reentrantLock.unlock();

        threadTest.CASTest.MyCASLock myCASLock = new threadTest.CASTest.MyCASLock();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                //myCASLock.lock();
                myCASLock.lockTimes(3);
                System.out.println(Thread.currentThread().getName() + " 被锁了,开始执行线程A的逻辑");
                TimeUnit.SECONDS.sleep(10);

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }finally {
                System.out.println(String.format("%s 当前线程的状态 %s", Thread.currentThread().getName(),
                                                 Thread.currentThread().getState()));
                myCASLock.unlock();
                System.out.println(String.format("%s 当前线程的状态 %s", Thread.currentThread().getName(),
                                                 Thread.currentThread().getState()));
            }
        },"T1").start();
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                //myCASLock.lock();
                myCASLock.lockTimes(3);
                System.out.println(Thread.currentThread().getName()+" 被锁了,开始执行线程B的逻辑");
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }finally {
                System.out.println(String.format("%s 当前线程的状态 %s", Thread.currentThread().getName(),
                                                 Thread.currentThread().getState()));
                myCASLock.unlock();
                System.out.println(String.format("%s 当前线程的状态 %s", Thread.currentThread().getName(),
                                                 Thread.currentThread().getState()));
            }
        },"T2").start();
    }
}
